package com.zhijiang.hospital.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhijiang.hospital.entity.PerformanceAssessment;
import com.zhijiang.hospital.entity.dto.PerformanceAssessmentDto;
import com.zhijiang.hospital.mapper.PerformanceAssessmentMapper;
import com.zhijiang.hospital.service.PerformanceAssessmentService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 75936
 * @version 1.0
 * @project zhijiang_hospital_data
 * @description
 * @date 2022-10-26 15:12:18
 */
@Service
public class PerformanceAssessmentServiceImpl implements PerformanceAssessmentService {

    @Autowired
    PerformanceAssessmentMapper performanceAssessmentMapper;


    @Override
    public List<PerformanceAssessmentDto> findAll() {
        return performanceAssessmentMapper.findAll();
    }

    @Override
    public Page<PerformanceAssessment> loadPageList(BaseQuery baseQuery) {
        QueryWrapper<PerformanceAssessment> performanceAssessmentQueryWrapper = new QueryWrapper<>();
        performanceAssessmentQueryWrapper.
                apply(StrUtil.isNotBlank(baseQuery.getStartTime()),
                        "create_time >= to_date('" + baseQuery.getStartTime() + "','yyyy-mm-dd hh24:mi:ss')").
                apply(StrUtil.isNotBlank(baseQuery.getEndTime()),
                        "create_time <= to_date('" + baseQuery.getEndTime() + "','yyyy-mm-dd hh24:mi:ss')").
                eq(StringUtils.isNotBlank(baseQuery.getDepartment()) && !baseQuery.getDepartment().equals("全部科室"), "dept_Name", baseQuery.getDepartment());
        return performanceAssessmentMapper.selectPage(new Page<>(baseQuery.getPage(), baseQuery.getPageSize()), performanceAssessmentQueryWrapper);
    }

    @Override
    public PerformanceAssessment loadOne(String id) {
        return performanceAssessmentMapper.selectById(id);
    }

    @Override
    public List<PerformanceAssessment> loadDept() {
        List list = performanceAssessmentMapper.selectList(null).stream().map(PerformanceAssessment::getDeptName).distinct().collect(Collectors.toList());
        return list;
    }

    @Override
    public List<PerformanceAssessment> loadDataYear() {
        DateTime dateTime = DateUtil.beginOfYear(new Date());
        String year = String.valueOf(DateUtil.year(dateTime));
        DateTime dateTime1 = DateUtil.endOfYear(new Date());
        String startTime = Convert.toStr(dateTime);
        String endTime = Convert.toStr(dateTime1);
        QueryWrapper<PerformanceAssessment> performanceAssessmentQueryWrapper = new QueryWrapper<>();
        performanceAssessmentQueryWrapper.apply(StrUtil.isNotBlank(startTime), "create_time >= to_date('" + startTime + "','yyyy-mm-dd hh24:mi:ss')").
                apply(StrUtil.isNotBlank(endTime), "create_time <= to_date('" + endTime + "','yyyy-mm-dd hh24:mi:ss')");
        List<PerformanceAssessment> list = performanceAssessmentMapper.selectList(performanceAssessmentQueryWrapper);
        return list;
    }

    @Override
    public List<PerformanceAssessment> loadDataLastYear() {
        Date nextYearPreDay = TimeUtil.getNextYearPreDay();
        DateTime dateTime3 = DateUtil.beginOfYear(nextYearPreDay);
        String lastYear = String.valueOf(DateUtil.year(dateTime3));
        DateTime dateTime4 = DateUtil.endOfYear(nextYearPreDay);
        String startTime = TimeUtil.format(dateTime3);
        String endTime = TimeUtil.format(dateTime4);
        QueryWrapper<PerformanceAssessment> performanceAssessmentQueryWrapper = new QueryWrapper<>();
        performanceAssessmentQueryWrapper.apply(StrUtil.isNotBlank(startTime), "create_time >= to_date('" + startTime + "','yyyy-mm-dd hh24:mi:ss')").
                apply(StrUtil.isNotBlank(endTime), "create_time <= to_date('" + endTime + "','yyyy-mm-dd hh24:mi:ss')");
        List<PerformanceAssessment> list = performanceAssessmentMapper.selectList(performanceAssessmentQueryWrapper);
        return list;
    }
}
